package per.kongtouxu.service.Impl;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import per.kongtouxu.dao.MenuDao;
import per.kongtouxu.domain.pojo.Menu;
import per.kongtouxu.domain.pojo.PageBean;
import per.kongtouxu.domain.vo.V_Admin;
import per.kongtouxu.service.MenuService;

import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Menu> findMenuByAdminId(Integer adminId) {
        List<Menu> menuList = null;

        try {
            menuList = menuDao.findMenuByAdminId(adminId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return menuList;
    }

    @Override
    public PageBean<Menu> findByPage(int currentPage, int pageSize) {
        //1.分页类
        PageBean<Menu> pageBean = new PageBean<>();
        //2.设置当前页码
        pageBean.setCurrentPage(currentPage);
        //3.设置每页显示条数
        pageBean.setPageSize(pageSize);
        //4.设置总记录条数
        int totalCount = menuDao.findCountByPage();
        pageBean.setTotalCount(totalCount);
        //5.设置PageBean中的实体数据
        //开始的记录条数=（当前页码-1）*每页显示条数
        int start = (currentPage - 1) * pageSize;
        List<Menu> lsit = menuDao.findByPage(new RowBounds(start, pageSize));
        pageBean.setList(lsit);
        //6.设置总页数   总页数= 总记录条数/每页显示条数=0 ？ 总记录条数/每页显示条数=0 ：（总记录条数/每页显示条数=0）+1
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        pageBean.setTotalPage(totalPage);
        //7.返回
        return pageBean;
    }

    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public void update(Menu menu) {
        menuDao.update(menu);
    }

    @Override
    public void delete(Integer menuId) {
        menuDao.delete(menuId);
    }

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public List<Menu> findMenuByRoleId(Integer roleId) {
        return menuDao.findMenuByRoleId(roleId);
    }
}
